package com.example.yls.mydemo;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
    private MyFramgentPageAdapter adapter;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    private TextView tv_4;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        bottomtvOnClick();
        initFragmentArrayList();


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tv_1.setBackgroundColor(Color.RED);
                        tv_2.setBackgroundColor(Color.GREEN);
                        tv_3.setBackgroundColor(Color.GREEN);
                        tv_4.setBackgroundColor(Color.GREEN);
                        break;
                    case 1:
                        tv_1.setBackgroundColor(Color.GREEN);
                        tv_2.setBackgroundColor(Color.RED);
                        tv_3.setBackgroundColor(Color.GREEN);
                        tv_4.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        tv_1.setBackgroundColor(Color.GREEN);
                        tv_2.setBackgroundColor(Color.GREEN);
                        tv_3.setBackgroundColor(Color.RED);
                        tv_4.setBackgroundColor(Color.GREEN);
                        break;
                    case 3:
                        tv_1.setBackgroundColor(Color.GREEN);
                        tv_2.setBackgroundColor(Color.GREEN);
                        tv_3.setBackgroundColor(Color.GREEN);
                        tv_4.setBackgroundColor(Color.RED);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void bottomtvOnClick() {

        tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        tv_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });
        tv_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(2);
            }
        });
        tv_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });

    }


    private void initFragmentArrayList() {
        MessageFragment messageFragment = new MessageFragment();
        ContactFragment contactFragment = new ContactFragment();
        FindFragment findFragment = new FindFragment();
        MyFragment myFragment = new MyFragment();

        fragmentArrayList.add(messageFragment);
        fragmentArrayList.add(contactFragment);
        fragmentArrayList.add(findFragment);
        fragmentArrayList.add(myFragment);

        adapter = new MyFramgentPageAdapter(getSupportFragmentManager(), fragmentArrayList);
        viewPager.setAdapter(adapter);
    }

    private void initView() {
        viewPager = findViewById(R.id.vp_content);
        tv_1 = findViewById(R.id.tv_1);
        tv_1.setBackgroundColor(Color.RED);
        tv_2 = findViewById(R.id.tv_2);
        tv_3 = findViewById(R.id.tv_3);
        tv_4 = findViewById(R.id.tv_4);
        tabLayout = findViewById(R.id.tl_tab);
        tabLayout.setupWithViewPager(viewPager);


    }
}
